﻿#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
Title = 签到系统 
Date = 2018-03-06
'''

from tornado import web,ioloop,httpserver
import time
from create_qr_code import get_code_by_str

#文件句柄
#全局变量

SIGN_FILE_HANDLER = open('sign.csv','a',encoding='utf-8')
SIGN_FILE_HANDLER.write('%s,%s,%s,%s\n' % ('姓名','学号','班级','签到时间'))

# 逻辑处理模块
class MainPageHandler(web.RequestHandler):
	def get(self,*args,**kwargs):
		#self.write('欢迎访问')
		self.render('index.html')

#签到模块		
class SignHandler(web.RequestHandler):

	def get(self,*args,**kwargs):
		#self.write('欢迎访问')
		self.render('sign.html')
		
	def post(self,*args,**kwargs):
		name = self.get_argument('name')
		num = self.get_argument('num')
		dep = self.get_argument('dep')
		SIGN_FILE_HANDLER.write('%s,%s,%s,%s\n' % (name,num,dep,str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))))
		SIGN_FILE_HANDLER.flush()
		self.write('签到成功')

#生成二维码
class CodeHandler(web.RequestHandler):
	def get(self,*args,**kwargs):
		#生成一个二维码图片
		#get_code_by_str
		code_img_hangler = get_code_by_str('http://xlj.ngrok.xiaomiqiu.cn/sign')
		self.write(code_img_hangler.getvalue())

# 路由		
application = web.Application([
	(r'/',MainPageHandler),
	(r'/sign',SignHandler),
	(r'/code',CodeHandler),
])


# socket 服务
if __name__ == '__main__':

	http_port = 4561

	http_server = httpserver.HTTPServer(application)
	http_server.listen(http_port)
	ioloop.IOLoop.current().start()
	
	